package com.xuecheng.system.controller;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.AuthInfo;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.commons.utils.JwtUtils;
import com.xuecheng.system.domain.Company;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xuecheng.system.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 教育机构前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CompanyController {

    @Autowired
    private CompanyService  companyService;


//    // 机构查询
//    @GetMapping("/company/mine")
//    public ResponseResult<CompanyVo> findCompany(@RequestHeader("Authorization") String token){  // 1.接收请求头
//        // 2.校验token合法性
//        Boolean verifyToken = JwtUtils.verifyToken(token);
//        if (verifyToken==false) {
//            throw new BusinessException(ErrorCode.NOTTOKEN);
//        }
//        // 3.解析token
//        AuthInfo authInfo = JwtUtils.getInfoFromToken(token);
//
//        if (authInfo.getCompanyId()==null) { // 如果不是企业用户，没有companyId
//            throw new BusinessException(ErrorCode.COMPANYERROR);
//        }
//        // 4.根据企业id查询企业对象
//        Company company = companyService.getById(authInfo.getCompanyId());
//        // 5.实体转vo
//        /*CompanyVo companyVo = new CompanyVo();
//        BeanUtils.copyProperties(company,companyVo);*/
//        CompanyVo companyVo = BeanHelper.copyProperties(company, CompanyVo.class);
//        // 6.返回结果
//        return ResponseResult.okResult(companyVo);
//    }

    // 机构查询
    @GetMapping("/company/mine")
    public ResponseResult<CompanyVo> findCompany(){  // 1.接收请求头
        //获取线程中的登录信息
        AuthInfo authInfo = AuthInfoHolder.getAuthInfo();
        if (authInfo.getCompanyId()==null) { // 如果不是企业用户，没有companyId
            throw new BusinessException(ErrorCode.COMPANYERROR);
        }
        // 4.根据企业id查询企业对象
        Company company = companyService.getById(authInfo.getCompanyId());
        // 5.实体转vo
        /*CompanyVo companyVo = new CompanyVo();
        BeanUtils.copyProperties(company,companyVo);*/
        CompanyVo companyVo = BeanHelper.copyProperties(company, CompanyVo.class);
        // 6.返回结果
        return ResponseResult.okResult(companyVo);
    }
}